<template >
    <div class="login_container" >
        <!-- 登录模块 -->
        <div class="login_box">
            <!-- 头像 -->
            <div class ="avatar_box">
                <img src="../assets/logo.png" alt/>
            </div>
            <!-- 表单区域 model为数据绑定 rules为数据校验绑定  -->
            <el-form :model="LoginForm" class="login_form"  :rules="loginrules" ref="LoginFormRef" label-width="0px" >
                <!-- 用户名称 -->
                <el-form-item label="" prop="username">
                    <!-- 进行数据绑定 -->
                    <el-input v-model="LoginForm.username" prefix-icon="iconfont icondenglu" ></el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item label="" prop="password" >
                    <!-- 进行数据绑定 -->
                    <el-input v-model="LoginForm.password" prefix-icon="iconfont iconmima" type="password"></el-input>
                </el-form-item>
                <!-- 按钮 -->
                <el-form-item class="btns " >
                    <el-button type="primary" @click="login" >提交</el-button>
                    <!-- <el-button type="success">成功按钮</el-button> -->
                    <el-button type="info" @click="resetLoginFrom">重置</el-button>
                </el-form-item>

            </el-form>
        </div>
    </div>
</template>
<script>
    export default{
        data(){
            return{
                // 数据
                LoginForm:{
                    username:"admin",
                    password:"123456"
                },
                // 校验规则
                loginrules:{
                    username: [
                            { required: true, message: '请输入用户名', trigger: 'blur' },//必填项，失去焦点时候触发
                            { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
                        ],
                        password: [
                          { required: true, message: '请输入密码', trigger: 'change' },
                          { min: 3, max: 8, message: '密码长度在 3 到 8 个字符', trigger: 'blur' }
                     ],
                }
            }
        },
        methods:{
            // 重置方法
            resetLoginFrom(){
                console.log("111");
                // this.$refs[formName].resetFields();
                this.$refs.LoginFormRef.resetFields();
            },
            // 处理登录的方法
             login(){
                 console.log(2222);
                 this.$refs.LoginFormRef.validate(async valid=>{//校验
                     if(!valid) return;
                     const {data: res}=await this.$http.post("login",this.LoginForm);//验证后台逻辑
                     if(res.flag=="ok"){//成功之后跳转页面
                        console.log("isiisis"+res.user.role);
                        // this.$message.success("操作成功！！！");
                        // 将user对象存储到session中
                        window.sessionStorage.setItem("user",res.user);
                        window.sessionStorage.setItem("role",res.user.role);

                        this.$router.push({path:"/home"});
                     }else{
                        this.$message.error("操作失败！！！");
                     }
                     console.log(res);
                 })
            },
     
},
    }



    
</script>

<style lang="less" scoped>
    .login_container{
        background-color: #2b4b6b;
        height: 100%;
    }
    .login_box{
        background-color: bisque;
        height: 300px;
        width: 450px;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        transform: translate(-50%,50%);
        .avatar_box{
            width: 130px; 
            height: 130px;
            border: 1px solid #eee;
            border-radius: 50%;
            padding: 5px;
            box-shadow: 0 0 2px #ddd ;
            position: absolute;
            left: 50%;
            transform: translate(-50%,-50%);
            background-color: #eee;
            img{
                width: 100%;
                height: 100%;
                border-radius: 50%;
                background-color: #eee;
            }
        }
    }
    .btns{
        display: flex;//流式布局
        justify-content: flex-end;
        position: absolute;
        left: 50%;
        bottom: -80%;
        transform: translate(-50%,-50%);
    }
    .login_form{
        position: absolute;
        bottom: 30%;
        width: 100%;
        padding: 0 10px;
        box-sizing: border-box;
    }
</style>